﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Bricks.Component.Utils;
using Bricks.Business.Models;
using Bricks.Business.Models.FlowProcess;
using Bricks.Site.Models;
using Bricks.Business.Data.Repositories.FlowProcess;
using System.Xml;
using Bricks.Business.Service;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(IFlowNodeService))]
    internal class FlowNodeService : IFlowNodeService
    {
        [Import]
        protected IFlowRepository FlowRepository { get; set; }
        [Import]
        protected IFlowNodeRepository FlowNodeRepository { get; set; }

        public List<FlowNodeView> GetFlowNode(string flowCode)
        {
            Flow flow = FlowRepository.Entities.Where(p => p.FlowCode == flowCode).First();
            IQueryable<FlowNode> flowNodeList = FlowNodeRepository.Entities.Where(p => p.PID == flow.ID);
            List<FlowNodeView> flowNodeViewList = new List<FlowNodeView>();
            foreach (FlowNode flowNode in flowNodeList)
            {
                flowNodeViewList.Add(new FlowNodeView()
                {
                    ID = flowNode.ID,
                    PID = flowNode.PID,
                    NodeCode = flowNode.NodeCode,
                    NodeName = flowNode.NodeName,
                    LocationX = flowNode.LocationX,
                    LocationY = flowNode.LocationY,
                    Width = flowNode.Width,
                    Height = flowNode.Height,
                    Shape = flowNode.Shape,
                    NodeXML = flowNode.NodeXML,
                    OrderNo = flowNode.OrderNo,
                    NodeLevel=flowNode.NodeLevel,
                    NodeType = flowNode.NodeType,
                    NodeSubType=flowNode.NodeSubType
                });
            }
            return flowNodeViewList;
        }

        public FlowNodeView GetFlowNodeByNodeCode(string flowCode, bool isRequestNode, string nodeCode)
        {
            Flow flow = FlowRepository.Entities.Where(p => p.FlowCode == flowCode).First();
            FlowNode flowNode = null;
            if (isRequestNode)
            {
                flowNode = FlowNodeRepository.Entities.Where(p => p.PID == flow.ID && p.NodeType == 1).First();
            }
            else
            {
                flowNode = FlowNodeRepository.Entities.Where(p => p.PID == flow.ID && p.NodeCode == nodeCode).First();
            }
            FlowNodeView flowNodeView = new FlowNodeView()
            {
                ID = flowNode.ID,
                PID = flowNode.PID,
                NodeCode = flowNode.NodeCode,
                NodeName = flowNode.NodeName,
                LocationX = flowNode.LocationX,
                LocationY = flowNode.LocationY,
                Width = flowNode.Width,
                Height = flowNode.Height,
                Shape = flowNode.Shape,
                NodeXML = flowNode.NodeXML,
                OrderNo = flowNode.OrderNo,
                NodeLevel = flowNode.NodeLevel,
                NodeType = flowNode.NodeType,
                NodeSubType = flowNode.NodeSubType
            };
            return flowNodeView;
        }
    }
}